import java.io.Serializable;
import java.lang.Character.Subset;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Stack;


public class Requete implements Serializable{
	private NoeudInterface site;
	private Stack<NoeudInterface> arborescence;
	
	public Requete(Noeud s, Stack<NoeudInterface> a){
		site = s;
		if(a != null)
		{
			arborescence = a;
		}
		else{
			arborescence = new Stack<NoeudInterface>();
		}
	}
	
	public Requete add(NoeudInterface a){
		arborescence.push(a);
		return this;
	}
	
	public NoeudInterface get(){
		if(!arborescence.isEmpty()){
			return arborescence.pop();
		}
		else{
			return site;
		}
		
	}
	
	public NoeudInterface getSite(){
		return site;
	}
	
	public NoeudInterface getFirst(){
		if(!arborescence.isEmpty()){
			return arborescence.peek();
		}
		else{
			return null;
		}
	}
	
	public void push(Noeud n){
		arborescence.push(n);
	}
	
	public String toString(){
		String retour = new String();
		try {
			retour += "(";
	
			retour += site.getNom();		
			retour += ",]";
			
			Iterator<NoeudInterface> i = arborescence.iterator();
			String arbo = new String();
			while(i.hasNext()){
				NoeudInterface n = i.next();
				arbo += n.getNom();
				arbo += ",";
			}
			if(arbo.length()>1){
				StringBuffer res = new StringBuffer(arbo.length());
			    for (int k = (arbo.length() - 2); k >= 0; k--)
			        res.append(arbo.charAt(k));        
				retour += res.toString();
			}
			retour += "])";
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retour;
	}
}
